package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.UpdateNotAllowException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealDishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult pageFind(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public void add(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.insert(setmeal);
        dto.getSetmealDishes().forEach(dish -> {
            dish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(dish);
        });
    }

    @Override
    public void delete(List<Long> ids) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids)
                .ne("status", 1);
        int delete = setmealMapper.delete(queryWrapper);
        if (delete != ids.size()) {
            throw new DeletionNotAllowedException("有" + (ids.size() - delete) + "条套餐正在起售无法删除！" + "已删除" + delete + "条套餐数" +
                    "据，请刷新页面");
        }
    }

    @Override
    public SetmealVO findById(Long id) {
        log.info("所查询的套餐的id为" + id);
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO vo = new SetmealVO();
        BeanUtils.copyProperties(setmeal, vo);
        //现在vo还需要查询setmeal dish表里面的信息并获取当前套餐内的餐品信息
        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id", id);
        setmealDishMapper.selectList(queryWrapper);
        vo.setSetmealDishes(setmealDishMapper.selectList(queryWrapper));
        return vo;
    }

    @Override
    public List<SetmealVO> findByCategoryId(Long categoryId) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        //queryWrapper.eq("status",StatusConstant.ENABLE);
        List<Setmeal> setmeals = setmealMapper.selectList(queryWrapper);
        List<SetmealVO> vo = setmeals.stream().
                map(setmeal -> {
                    SetmealVO v = new SetmealVO();
                    BeanUtils.copyProperties(setmeal, v);
                    return v;
                })
                .filter(v -> v.getStatus().equals(StatusConstant.ENABLE))
                .collect(Collectors.toList());
        return vo;
    }

    @Override
    public List<SetmealDishVO> getDishs(Integer id) {
        return setmealDishMapper.getDishes(id);
    }

    @Override
    public void update(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.updateById(setmeal);
        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id", dto.getId());
        setmealDishMapper.delete(queryWrapper);
        dto.getSetmealDishes().forEach(d -> {
            d.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(d);
        });
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        if (status == 1) {//准备起售套餐
            int i = setmealDishMapper.fromSetmealToSetmealDishToDish(id);

            QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("setmeal_id", id);
            Integer i1 = setmealDishMapper.selectCount(queryWrapper);
            log.info("当前套餐内菜品个数为{}", i1);
            log.info("当前套餐内起售的菜品个数为{}", i);
            if (i1 == i) {
                Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
                setmealMapper.updateById(setmeal);
            } else {
                throw new UpdateNotAllowException("套餐中有未起售餐品！");
            }

        } else { //准备关闭起售的套餐
            Setmeal setmeal = Setmeal.builder().
                    status(status).
                    id(id).
                    build();
            setmealMapper.updateById(setmeal);
        }
    }

}
